using Godot;
using System;

public partial class RobotDetailInfo : Node2D
{
	Tween tween;
	bool showing;

	#region ui控件
	CanvasLayer canvasLayer;
	PanelContainer panelRoot;
	Button buttonCancel;
	TextureRect robotPortrail;
	Label robotName;
	Label robotLevel;
	Label robotMoveType;
	Label movement;
	Label sp;
	Label maxSp;
	Label attack;
	Label defense;
	Label speed;
	Label hp;
	Label maxHp;
	Label currentExp;
	Label nextLevelExp;
	TextureRect avatar;
	Label pilotName;

	Label weaponName1;
	Label hitRate1;
	Label range1;
	Label OValue1;
	Label LValue1;
	Label AValue1;

	Label weaponName2;
	Label hitRate2;
	Label range2;
	Label OValue2;
	Label LValue2;
	Label AValue2;
	#endregion

	public override void _Ready()
	{
		GetChildNodes();
	}

	private void GetChildNodes()
	{
		canvasLayer = GetNode<CanvasLayer>("%CanvasLayer");
		canvasLayer.Show();
		panelRoot = GetNode<PanelContainer>("%Root");
		panelRoot.Scale = Vector2.Zero;
		buttonCancel = GetNode<Button>("%ButtonCancel");
		buttonCancel.Pressed += CancelFunc;
		robotPortrail = GetNode<TextureRect>("%RobotPortrail");
		robotName = GetNode<Label>("%RobotName");
		robotLevel = GetNode<Label>("%RobotLevel");
		robotMoveType = GetNode<Label>("%RobotMoveType");
		movement = GetNode<Label>("%Movement");
		sp = GetNode<Label>("%SP");
		maxSp = GetNode<Label>("%MaxSP");
		attack = GetNode<Label>("%Attack");
		defense = GetNode<Label>("%Def");
		speed = GetNode<Label>("%Speed");
		hp = GetNode<Label>("%HP");
		maxHp = GetNode<Label>("%MaxHP");
		currentExp = GetNode<Label>("%CurrentExp");
		nextLevelExp = GetNode<Label>("%NextLevelExp");
		avatar = GetNode<TextureRect>("%Avatar");
		pilotName = GetNode<Label>("%PilotName");

		// 武器
		weaponName1 = GetNode<Label>("%WeaponName1");
		hitRate1 = GetNode<Label>("%HitRate1");
		range1 = GetNode<Label>("%Range1");
		OValue1 = GetNode<Label>("%OValue1");
		LValue1 = GetNode<Label>("%LValue1");
		AValue1 = GetNode<Label>("%AValue1");
		weaponName2 = GetNode<Label>("%WeaponName2");
		hitRate2 = GetNode<Label>("%HitRate2");
		range2 = GetNode<Label>("%Range2");
		OValue2 = GetNode<Label>("%OValue2");
		LValue2 = GetNode<Label>("%LValue2");
		AValue2 = GetNode<Label>("%AValue2");
	}

	private void CancelFunc()
	{
		AnimateHide();
	}

	string none = "无";
	string zero = "--";

	public void UpdateInfo(Robot robot)
	{
		robotPortrail.Texture = GameUtil.GetRobotPortrait(robot.Entity.Portrait);
		robotName.Text = robot.Entity.RobotName;
		robotLevel.Text = robot.Entity.Level.ToString();
		robotMoveType.Text = GameUtil.TransRobotType(robot.Entity.MoveType);
		movement.Text = robot.Entity.Movement.ToString();
		sp.Text = robot.Entity.CurrentSprite.ToString();
		maxSp.Text = robot.Entity.TotalSprite.ToString();
		attack.Text = robot.Entity.TotalAttack.ToString();
		defense.Text = robot.Entity.TotalDefense.ToString();
		speed.Text = robot.Entity.TotalSpeed.ToString();
		hp.Text = robot.Entity.CurrentHealth.ToString();
		maxHp.Text = robot.Entity.TotalHealth.ToString();
		currentExp.Text = robot.Entity.Exp.ToString();
		nextLevelExp.Text = $"{robot.Entity.MaxExp - robot.Entity.Exp}";
		avatar.Texture = GameUtil.GetPilotAvatar(robot.Entity.Pilot.Avatar);
		pilotName.Text = robot.Entity.Pilot.PilotName;
		// 武器
		var wps = robot.Entity.WeaponList;
		if (wps.Count > 0)
		{
			weaponName1.Text = wps[0].WeaponName;
			hitRate1.Text = wps[0].HitRate.ToString();
			range1.Text = wps[0].RangeMax.ToString();
			OValue1.Text = wps[0].AttackOcean.ToString();
			LValue1.Text = wps[0].AttackLand.ToString();
			AValue1.Text = wps[0].AttackAir.ToString();
		}
		else
		{
			weaponName1.Text = none;
			hitRate1.Text = zero;
			range1.Text = zero;
			OValue1.Text = zero;
			LValue1.Text = zero;
			AValue1.Text = zero;
		}

		if (wps.Count > 1)
		{
			weaponName2.Text = wps[1].WeaponName;
			hitRate2.Text = wps[1].HitRate.ToString();
			range2.Text = wps[1].RangeMax.ToString();
			OValue2.Text = wps[1].AttackOcean.ToString();
			LValue2.Text = wps[1].AttackLand.ToString();
			AValue2.Text = wps[1].AttackAir.ToString();
		}
		else
		{
			weaponName2.Text = none;
			hitRate2.Text = zero;
			range2.Text = zero;
			OValue2.Text = zero;
			LValue2.Text = zero;
			AValue2.Text = zero;
		}
	}

	public void AnimateShow(Robot robot)
	{
		UpdateInfo(robot);
		tween?.Stop();
		// GameUtils.LockGameInput();
		tween = CreateTween();
		tween.TweenProperty(panelRoot, "scale", Vector2.One, 0.2f);
		tween.Finished += TweenFinished;
		showing = true;
	}

	public void AnimateHide()
	{
		tween?.Stop();
		tween = CreateTween();
		tween.TweenProperty(panelRoot, "scale", Vector2.Zero, 0.2f);
		tween.Finished += TweenFinished;
		showing = false;
	}

	private void TweenFinished()
	{
		if (!showing)
		{
			// GameUtils.UnlockGameInput();
		}
	}

}
